import pygame
import Graph
import Vertex
import Algorithm
import utils
import sys
import me as pointer

def main():
	pygame.init()
	screen = pygame.display.set_mode((1024, 768), pygame.DOUBLEBUF)
	pygame.display.set_caption('Nav')
	
	
	font = pygame.font.Font(None, 20)
	
	vertice_image, rect = utils.load_image("vertice.png",(255,255,255))
	g = Graph.Graph("grafo1.graph")
	vertices = g.get_vertices()

	vertices2 = []
	for vertex in vertices:
		text = font.render(vertex.get_name(), 1, (255,255,255))
		x,y = vertex.get_pos()
		x-= text.get_width()/2
		y-= text.get_height()/2
		vertices2.append((vertex.get_pos(), text, (x,y)))

	drawed = []
	pesos = []
	aristas = []
	for vertex in vertices:
		for adjacent in vertex.get_adjacents():
			x,y = vertex.get_pos()
			z,t = adjacent.get_pos()
			if member(drawed, (x,y,z,t)) or member(drawed, (z,t,y,x)):
				continue
			drawed+=[(x,y,z,t)]
			#x+= vertice_image.get_width()/2 ; z+= vertice_image.get_width()/2
			#y+= vertice_image.get_height()/2 ; t+= vertice_image.get_height()/2
			aristas.append(((x,y),(z,t)))
			text = font.render(str(g.get_weight(vertex.get_name(),adjacent.get_name())), 1, (0,0,0))
			posx = (x+z) / 2
			posy = (y+t) / 2
			pesos.append((text,(posx,posy)))

	path = Algorithm.Dijkstra(g, "a", "h")
	dst = "h"
	
	me = pointer.Me(g.find_by_name("a"))
	
	while True:
		pygame.time.Clock().tick(40)

		for event in pygame.event.get():
			if event.type == pygame.QUIT:
				sys.exit()
				
		if pygame.key.get_pressed()[pygame.K_d]:
			print "Dijkstra:"
			src = raw_input("vertice origen: ")
			dst = raw_input("vertice destino: ")
			path = Algorithm.Dijkstra(g, src, dst)
		if pygame.key.get_pressed()[pygame.K_LEFT]:
			me.rotate_left()
			pygame.time.wait(100)
		if pygame.key.get_pressed()[pygame.K_RIGHT]:
			me.rotate_right()
			pygame.time.wait(100)
		if pygame.key.get_pressed()[pygame.K_UP]:
			me.go_next()
			pygame.time.wait(100)
			
		if not member(path, me.vertex):
			path = Algorithm.Dijkstra(g, me.vertex.get_name(), dst)
		
		screen.fill((255,255,255))
		for arista in aristas:
			pygame.draw.line(screen, (0,255,0), arista[0],arista[1], 3)
			
		vertex = path[1]
		i = 2
		while i < len(path):
			vertex2 = path[i]
			x,y = vertex.get_pos()
			z,t = vertex2.get_pos()
			#x+= vertice_image.get_width()/2 ; z+= vertice_image.get_width()/2
			#y+= vertice_image.get_height()/2 ; t+= vertice_image.get_height()/2
			pygame.draw.line(screen, (0,0,255), (x,y),(z,t), 3)
			vertex = vertex2
			i+=1
						
			
		for vertex in vertices2:
			pygame.draw.circle(screen,(255,0,0),vertex[0],12)
			screen.blit(vertex[1], vertex[2])

		for i in pesos:
			pygame.draw.circle(screen,(255,255,255),i[1],10)
			screen.blit(i[0],(i[1][0]-i[0].get_width()/2,i[1][1]-i[0].get_height()/2))
		screen.blit(me.image, me.rect)
		pygame.display.flip()
		
	

def member(visited, vertex):
	"""
	return true if the vertex is member of the visited list
	"""
	for i in visited:
		if i == vertex:
			return True
	return False
	
if __name__ == "__main__":
	main()	
